#include "widget.h"
#include "ui_widget.h"
#include<QTimer>
#include<QKeyEvent>
#include<qpushbutton.h>
#include<QToolButton>
#include"bullet.h"
#include<QGraphicsPixmapItem>
#include"score.h"


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{

    ui->setupUi(this);
    this->setFixedSize(480,700);

    mGameview.setSceneRect(QRect(0,0,480,700));
    mScene.setSceneRect(QRect(0,0,480,700));
    mStartScence.setSceneRect(QRect(0,0,480,700));
    mStartScence.addPixmap(QPixmap(":/img/C:/Users/lenovo/Desktop/pic/background.png"));//开始背景界面的路径
    mGameOverScence.setSceneRect(QRect(0,0,480,700));
    mGameOverScence.addPixmap(QPixmap(":/img/C:/Users/lenovo/Desktop/pic/background.png"));

    auto startBtn=new QToolButton();
    startBtn->setAutoRaise(true);
    startBtn->resize(60,45);
    startBtn->setIcon(QIcon(":/img/C:/Users/lenovo/Desktop/pic/resume_pressed.png"));//开始的图片路径
    startBtn->setIconSize(QSize(60,45));
    startBtn->move(195,334);
    connect(startBtn,&QToolButton::clicked,[this](){
        mGameview.setScene(&mScene);
        mGameview.show();
    });





    mStartScence.addWidget(startBtn);

    mBackGround1.setPixmap(QPixmap(":/img/C:/Users/lenovo/Desktop/pic/background.png"));//背景图片1的路径
    mBackGround2.setPixmap(QPixmap(":/img/C:/Users/lenovo/Desktop/pic/background.png"));//背景图片2的路径

    mPlane.setPixmap(QPixmap(":/img/C:/Users/lenovo/Desktop/pic/plane修改尺寸.png"));    //飞机图片路径

    mBackGround2.setPos(0,-mBackGround1.pixmap().height());
    mPlane.setPos(250,600);  //飞机的初始位置



    mScene.addItem(&mBackGround1);
    mScene.addItem(&mBackGround2);
    mScene.addItem(&mPlane);
    mGameview.setScene(&mScene);
    mGameview.setScene(&mStartScence);
    score=new Score();
    mScene.addItem(score);
    score->setPos(10,10);



    mGameview.setParent(this);
    mGameview.show();


    mBGMoveTimer=new QTimer(this);
    mBGMoveTimer->start(40);
    connect(mBGMoveTimer,&QTimer::timeout,this,&Widget::BGMove);

    mPlaneMoveTimer=new QTimer(this);
    mPlaneMoveTimer->start(10);
    connect(mPlaneMoveTimer,&QTimer::timeout,this,&Widget::PlaneMove);


    mPlaneShootTimer=new QTimer(this);
    mPlaneShootTimer->start(400);//子弹的射击速度
    connect(mPlaneShootTimer,&QTimer::timeout,this,&Widget::PlaneBulletShoot);

    mBulletMoveTimer=new QTimer(this);
    mBulletMoveTimer->start(10);
    connect(mBulletMoveTimer,&QTimer::timeout,[this](){
        for(auto bullet : mBUlletList)
        {
            bullet->BulletMove();

        }
        Collision();
    });

    mEnemyCreatTimer=new QTimer(this);
    mEnemyCreatTimer->start(800);//敌机生成的速度
    connect(mEnemyCreatTimer,&QTimer::timeout,this,&Widget::CreatEnemy);

    mEnemyMoveTimer=new QTimer(this);
    mEnemyMoveTimer->start(4);//敌机移动速度
    connect(mEnemyMoveTimer,&QTimer::timeout,[this](){
        for(auto enemy : mEnemyList)
        {
            enemy->EnemyMove();

        }

        Collision();

    });




}



Widget::~Widget()
{
    delete ui;
}
void Widget::BGMove()
{

    mBackGround1.moveBy(0,5);
    mBackGround2.moveBy(0,5);

    if(mBackGround1.y()>mBackGround1.pixmap().height())
    {
        mBackGround1.setY(-mBackGround1.pixmap().height());
    }
    else if(mBackGround2.y()>=mBackGround2.pixmap().height())
    {
        mBackGround2.setY(-mBackGround2.pixmap().height());
    }


}


void Widget::PlaneMove()
{
    for(int keyCode : mKeyList)
    {
        switch (keyCode)
        {
        case Qt::Key_W: mPlane.moveBy(0,-3);break;
        case Qt::Key_A: mPlane.moveBy(-3,0);break;
        case Qt::Key_S: mPlane.moveBy(0,3);break;
        case Qt::Key_D: mPlane.moveBy(3,0);break;    //可以调节移动速度

        }
    }
    if(mPlane.x()<0)
    {
        mPlane.setX(0);
    }
    if(mPlane.y()<0)
    {
        mPlane.setY(0);
    }
    if(mPlane.x()>this->width()-mPlane.pixmap().width())
    {
        mPlane.setX(this->width()-mPlane.pixmap().width());
    }
    if(mPlane.y()>this->height()-mPlane.pixmap().height())
    {
        mPlane.setY(this->height()-mPlane.pixmap().height());
    }
}


void Widget::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {

        case Qt::Key_W:
        case Qt::Key_A:
        case Qt::Key_S:
        case Qt::Key_D:
        mKeyList.append(event->key());
        break;




    }

}

void Widget::keyReleaseEvent(QKeyEvent *event)
{

    if(mKeyList.contains(event->key()))
    {
        mKeyList.removeOne(event->key());

    }

}

void Widget::PlaneBulletShoot()
{
    QPixmap bulletImg(":/img/C:/Users/lenovo/Desktop/pic/bullet1.png");//子弹的路径
    QPoint pos(mPlane.x()+mPlane.pixmap().width()/2-bulletImg.width(),mPlane.y());
    Bullet *bullet=new Bullet(pos,bulletImg,Bullet::BT_Player);

    mScene.addItem(bullet);

    mBUlletList.append(bullet);


}



void Widget::CreatEnemy()
{

    QPixmap pixmap(":/img/C:/Users/lenovo/Desktop/pic/敌机.png");
    int randx=qrand()%(480-pixmap.width());
    Enemy* enemy=new Enemy(QPoint(randx,-10),pixmap);

    mScene.addItem(enemy);

    mEnemyList.append(enemy);


}


void Widget::Collision()
    {
    for(int i=0;i<mBUlletList.size();i++){
        for(int j=0;j<mEnemyList.size();j++)
        {
            if(mBUlletList[i]->collidesWithItem(mEnemyList[j]))//碰撞检测
            {
                //移除场景
                mScene.removeItem(mEnemyList[j]);
                mScene.removeItem(mBUlletList[i]);

                //移除管理器
                mEnemyList.removeOne(mEnemyList[j]);
                mBUlletList.removeOne(mBUlletList[i]);
                score->increase();



            }
        }
    }
}

void Widget::PlaneCollision()
{
    for(int i=0;i<mEnemyList.size();i++)
    {
        if(mEnemyList[i]->collidesWithItem(&mPlane))
        {
            auto overBtn=new QToolButton();
            overBtn->setAutoRaise(true);
            overBtn->resize(300,41);
            overBtn->setIcon(QIcon(":/img/C:/Users/lenovo/Desktop/pic/gameover.png"));//结束的图片路径
            overBtn->setIconSize(QSize(300,41));
            overBtn->move(195,334);
            connect(overBtn,&QToolButton::clicked,[this](){
                mGameview.setScene(&mScene);
                mGameview.show();

        });
    }
}
}





